<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{number}}.{{title}}</title>
    <!-- 引入ACE插件 -->
    <!-- 官网链接：https://ace.c9.io/ -->
    <!-- CDN链接：https://cdnjs.cloudflare.com/libraries/ace -->
    <!-- 使用介绍：https://www.iteye.com/blog/ybc77107-2296261 -->
    <!-- https://justcode.ikeepstudying.com/2016/05/ace-editor-%E5%9C%A8%E7%BA%BF%E4%BB%A3%E7%A0%81%E7%BC%96%E8%BE%91%E6%9E%81%E5%85%B6%E9%AB%98%E4%BA%AE/ -->
    <!-- 引入ACE CDN -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript" charset="utf-8"></script>
    <!-- 引入jquery CDN -->
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <!-- 引入 Tailwind CSS -->
    <script src="https://unpkg.com/@tailwindcss/browser@4"></script>
    <!-- 引入 Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <style>
        /* 定义提示框样式 */
       .success-alert {
            position: fixed;
            top: -50px;
            left: 50%;
            transform: translateX(-50%);
            background-color: #4CAF50;
            color: white;
            padding: 15px;
            border-radius: 5px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            z-index: 999;
            transition: top 0.3s ease-in-out;
        }

       .success-alert.show {
            top: 20px;
        }

        /* 设置编辑器容器的固定高度 */
       .editor-container {
            height: 600px;
        }

        /* 添加背景图样式 */
        body {
            background-image: url('./2.png');
            background-size: cover;
            background-repeat: no-repeat;
            background-attachment: fixed;
        }

        /* 题目描述和编辑器容器的背景设置为半透明白色，防止背景图影响内容阅读 */
       .bg-white {
            background-color: rgba(255, 255, 255, 0.8);
        }

        /* 设置 Ace 编辑器的 z-index 以确保不遮挡背景图 */
       .ace_editor {
            z-index: 1;
        }

        /* 导航栏和提示框保持较高的 z-index */
        nav,
       .success-alert {
            z-index: 2;
        }
    </style>
</head>

<body class="bg-gray-100 min-h-screen">
    <!-- 提示框 -->
    <div class="success-alert" id="success-alert">提交成功</div>
    <!-- 导航栏 -->
    <nav class="bg-black shadow-md">
        <div class="container mx-auto px-4 py-3 flex justify-between items-center">
            <div class="flex space-x-4">
                <a href="/" class="text-white text-lg hover:text-green-400 transition duration-300">首页</a>
                <a href="/all_questions" class="text-white text-lg hover:text-green-400 transition duration-300">题库</a>
                <a href="#" class="text-white text-lg hover:text-green-400 transition duration-300">竞赛</a>
                <a href="#" class="text-white text-lg hover:text-green-400 transition duration-300">讨论</a>
                <a href="#" class="text-white text-lg hover:text-green-400 transition duration-300">求职</a>
            </div>
            <a href="#" class="text-white text-lg hover:text-green-400 transition duration-300">登录</a>
        </div>
    </nav>
    <!-- 主体内容 -->
    <div class="container mx-auto px-4 py-8">
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <!-- 题目描述 -->
            <div class="bg-white rounded-lg shadow p-4 overflow-y-auto h-full">
                <h3 class="text-xl font-bold mb-2"><span id="number">{{number}}</span>.{{title}}_{{level}}</h3>
                <pre class="text-gray-700">{{desc}}</pre>
            </div>
            <!-- 代码编辑器 -->
            <div class="bg-white rounded-lg shadow editor-container">
                <pre id="code" class="ace_editor h-full"><textarea class="ace_text-input">{{header}}</textarea></pre>
            </div>
        </div>
        <!-- 提交按钮和结果显示 -->
        <div class="flex flex-wrap justify-between items-center mt-4 gap-4">
            <div class="bg-white rounded-lg shadow p-4 grow md:grow-0 md:max-w-md overflow-x-auto overflow-y-auto">
                <div class="result whitespace-pre-wrap break-words"></div>
            </div>
            <button class="btn-submit bg-green-500 text-white py-3 px-6 rounded-lg text-xl hover:bg-green-600 transition duration-300"
                onclick="submit()">提交代码</button>
        </div>
    </div>
    <script>
        //初始化对象
        editor = ace.edit("code");

        //设置风格和语言（更多风格和语言，请到github上相应目录查看）
        // 主题大全：http://www.manongjc.com/detail/25-cfpdrwkkivkikmk.html
        //editor.setTheme("ace/theme/monokai");
        editor.setTheme("ace/theme/twilight");
        editor.session.setMode("ace/mode/c_cpp");

        // 字体大小
        editor.setFontSize(16);
        // 设置默认制表符的大小:
        editor.getSession().setTabSize(4);

        // 设置只读（true时只读，用于展示代码）
        editor.setReadOnly(false);

        // 启用提示菜单
        ace.require("ace/ext/language_tools");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });

        function submit() {
            // alert("嘿嘿!");
            // 1. 收集当前页面的有关数据, 1. 题号 2.代码
            var code = editor.getSession().getValue();
            // console.log(code);
            var number = $(".container .grid .bg-white:first h3 #number").text();
            // console.log(number);
            var judge_url = "/judge/" + number;
            // console.log(judge_url);
            // 2. 构建json，并通过ajax向后台发起基于http的json请求
            $.ajax({
                method: 'Post',
                url: judge_url,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({
                    'code': code,
                    'input': ''
                }),
                success: function (data) {
                    //成功得到结果
                    // console.log(data);
                    show_result(data);
                    // 显示提交成功提示
                    showSuccessAlert();
                }
            });
            // 3. 得到结果，解析并显示到 result中
            function show_result(data) {
                // console.log(data.status);
                // console.log(data.reason);
                // 拿到result结果标签
                var result_div = $(".container .flex .bg-white .result");
                // 清空上一次的运行结果
                result_div.empty();

                // 首先拿到结果的状态码和原因结果
                var _status = data.status;
                var _reason = data.reason;

                var reason_lable = $("<p>", {
                    text: _reason
                });
                reason_lable.appendTo(result_div);

                if (_status == 0) {
                    // 请求是成功的，编译运行过程没出问题，但是结果是否通过看测试用例的结果
                    var _stdout = data.stdout;
                    var _stderr = data.stderr;

                    var stdout_lable = $("<pre>", {
                        text: _stdout
                    });

                    var stderr_lable = $("<pre>", {
                        text: _stderr
                    })

                    stdout_lable.appendTo(result_div);
                    stderr_lable.appendTo(result_div);
                } else {
                    // 编译运行出错,do nothing
                }
            }
        }

        function showSuccessAlert() {
            var alert = document.getElementById('success-alert');
            alert.classList.add('show');
            // 3秒后隐藏提示框
            setTimeout(function () {
                alert.classList.remove('show');
            }, 3000);
        }
    </script>
</body>

</html>    